#!/usr/bin/evn python
# coding=utf-8
from typing import List, Dict

from infirmary_admin_src.infirmary_smart_admin.infirmary_models.admin_dbhelper import session_scope, \
    session_scope_atomic,fn
from infirmary_admin_src.infirmary_smart_admin.infirmary_models.admin_model import SysEmployee, SysMenu,SysEmployee,SysRoleEmployee,SysRoleMenu,SysDept

class LogicBusinesses:
    pass

    @staticmethod
    def get_sys_employee_info_by_login_name(login_name) -> SysEmployee:
        with session_scope():
            return SysEmployee.get_or_none(SysEmployee.login_name == login_name, SysEmployee.deleted_flag == 0)



    @staticmethod
    def get_sys_employee_info_by_login_name_deptid(login_name) -> SysEmployee:
        with session_scope():
            return SysEmployee.select(SysDept.department_code).join(
                SysDept,on=(SysDept.department_id==SysEmployee.department_id)
            ).dicts().where(SysEmployee.login_name == login_name,
                    SysEmployee.deleted_flag == 0)

    @staticmethod
    def get_sys_employee_menuList_by_admin_role() -> List[SysMenu]:
        with session_scope():


            return [v for v in SysMenu.select().order_by(SysMenu.sort.asc()).where(SysMenu.deleted_flag == 0).dicts()]

    @staticmethod
    def get_sys_employee_menuList_by_admin_role_byEmployeeid(employee_id) -> List[SysMenu]:
        with session_scope():
            # 获取用户的所有的角色ID
            user_roles = SysRoleEmployee.select(SysRoleEmployee.role_id).dicts().where(SysRoleEmployee.employee_id==employee_id)
            if not user_roles:
                return []
            user_roles_ids = [v.get('role_id') for v in user_roles]
            # 查询当前角色ID相关角色所拥有的菜单ID
            role_menus = SysRoleMenu.select(SysRoleMenu.menu_id).dicts().where(SysRoleMenu.role_id.in_(user_roles_ids))
            if not role_menus:
                return []
            # 去重菜单ID
            role_menus_ids = list(dict.fromkeys([v.get('menu_id') for v in role_menus]))
            return [v for v in SysMenu.select().order_by(SysMenu.sort.asc()).where(SysMenu.menu_id.in_(role_menus_ids),SysMenu.deleted_flag == 0).dicts()]

    @staticmethod
    def get_sys_employee_authorities_by_admin_role_byEmployeeid(employee_id) -> List[Dict]:
        with session_scope():
            # 获取用户的所有的角色ID
            user_roles = SysRoleEmployee.select(SysRoleEmployee.role_id).dicts().where(
                SysRoleEmployee.employee_id == employee_id)
            if not user_roles:
                return []
            user_roles_ids = [v.get('role_id') for v in user_roles]
            # 查询当前角色ID相关角色所拥有的菜单ID
            role_menus = SysRoleMenu.select(SysRoleMenu.menu_id).dicts().where(SysRoleMenu.role_id.in_(user_roles_ids))
            if not role_menus:
                return []
                # 去重菜单ID
            role_menus_ids = list(dict.fromkeys([v.get('menu_id') for v in role_menus]))
            return [{
                'authority': v.get('web_perms')
            } for v in SysMenu.select(SysMenu.web_perms).where(SysMenu.menu_type == 3,
                                                               SysMenu.menu_id.in_(role_menus_ids),
                                                               SysMenu.deleted_flag == 0,
                                                               SysMenu.disabled_flag == 0,).dicts().order_by(
                SysMenu.create_time.desc())]

    @staticmethod
    def get_sys_employee_authorities_by_admin_role() -> List[Dict]:
        with session_scope():
            return [{
                'authority': v.get('web_perms')
            } for v in SysMenu.select(SysMenu.web_perms).where(SysMenu.menu_type == 3,
                                                               SysMenu.disabled_flag == 0,
                                                               SysMenu.deleted_flag ==0,
                                                               ).dicts().order_by(
                SysMenu.create_time.desc())]

    @staticmethod
    def get_sys_employee_menuList_only_menu() -> List[SysMenu]:
        with session_scope():
            return [v for v in SysMenu.select().where(SysMenu.deleted_flag == 0,
                                                      SysMenu.menu_type != 3,
                                                      SysMenu.deleted_flag == 0,
                                                      ).dicts()]

    @classmethod
    def edit_sys_menu_batch(cls, ids=None, **kwargs):
        with session_scope():
            print("输出IDs：", ids)
            return SysMenu.update(**kwargs).where(SysMenu.menu_id.in_(ids)).execute()

    @classmethod
    def edit_sys_menu_singe(cls, menu_id=None, **kwargs):
        with session_scope():
            return SysMenu.update(**kwargs).where(SysMenu.menu_id==menu_id).execute()




    @classmethod
    def get_sys_menu_id_max_id(cls):
        '''
        添加职务
        :param role_id:
        :return:
        '''
        with session_scope():
            return SysMenu.select(fn.MAX(SysMenu.menu_id)).scalar()

    @classmethod
    def add_sys_menu(cls, **kwargs):
        '''
        添加职务
        :param role_id:
        :return:
        '''
        with session_scope():
            return SysMenu.get_or_create(**kwargs)


    @staticmethod
    def get_sys_employee_List_by_admin_role() -> List[SysEmployee]:
        with session_scope():
            return [v for v in SysEmployee.select().where(SysEmployee.deleted_flag == 0).dicts()]

if __name__ == '__main__':
   print(LogicBusinesses.get_sys_employee_authorities_by_admin_role())